from jinja2 import Environment, FileSystemLoader, select_autoescape
import json
import getpass
import subprocess
from datetime import date, datetime,timedelta
import os
import re
from prettytable import PrettyTable
from util import git_root_path

def get_capture_time(trading_date=(date.today()-timedelta(days=1)).strftime('%Y%m%d')):
    env = Environment(
        loader=FileSystemLoader(f"{git_root_path}/python/coin/experimental/ziyan/fast_feed_tool"),
        autoescape=select_autoescape()
    )
    template = env.get_template("driver_json_file.tmpl")
    
    data_root_path = '/remote/iosg/coin-2/feed_cache2/feed-10.ap-northeast-1.aws/'

    ref_date = (datetime.strptime(trading_date,'%Y%m%d')-timedelta(days=1)).strftime('%Y%m%d')
    ref_path = os.path.join(data_root_path,ref_date)
    search_path = os.path.join(data_root_path,trading_date)
    ref_list = get_verify_list(ref_path)
    verify_list = get_verify_list(search_path)

    for mea,products in verify_list.items():
        if mea in ref_list.keys():
            verify_list[mea]=list(verify_list[mea] - ref_list[mea])
        else:
            verify_list[mea]=list(verify_list[mea])
        if len(verify_list[mea]) > 0:
            print("%s: %s" %(mea,verify_list[mea].__str__()))

    for mea,products in verify_list.items():
        if len(products) ==0:
            continue
        if 'Binance' in mea :
            archive_type = 'RAW'
            worker = 1
        else:
            archive_type = 'FASTFEED'
            worker = 0
        json_str=template.render(mea=mea, products=products.__str__().replace("'", '"'),archive_type=archive_type,worker=worker,trading_date=trading_date)

        print(json_str)
        with open(f'{git_root_path}/cc/experimental/ziyan/verify_config.json','w') as f:
            json.dump(json.loads(json_str),f)
        command = f"cd {git_root_path}; GLOG_logtostderr=1 bazel-out/k8-opt/bin/cc/experimental/ziyan/capture_time_picker --driver=cc/experimental/ziyan/verify_config.json"
        ret = subprocess.run(command, capture_output=True, shell=True)
        if ret.returncode != 0 :
            print(ret.stderr.decode())
            ret.check_returncode()
    print_summary()

def get_verify_list(path):
    verify_list={}
    for cur_dir,dirs,files in os.walk(path):
        if len(files) !=0:
            mea = extract_mea(cur_dir)
            if mea and (mea not in verify_list.keys()):
                verify_list[mea] = files_to_product(files)
            else:
                continue
    return verify_list

def extract_mea(path:str):
    mear = path.split('/')[-1]
    # index,non_tbs will have feed before start trading
    if mear.split('.')[-1] not in ['index','non_tbs'] :
        return '.'.join(mear.split('.')[:-1])

def files_to_product(files):
    ignore_products = {'ETH-USD', 'BTC-USD', 'ETH-USDT', 'BTC-USDT', 'ETH-USD.PERPETUAL', 'BTC-USD.PERPETUAL', 'ETH-USDT.PERPETUAL', 'BTC-USDT.PERPETUAL'}
    for n in range(len(files)):
        # remove'.W0.lz4'
        files[n] = files[n][:-7]
        # remove 'yyyymmddThhmmss'
        if re.match(r'.+[0-9]T+[0-9]',files[n]) is not None:
            files[n] = files[n][:-16]
    return set(files)-ignore_products


def print_summary():
    path = f'{git_root_path}/../ops_file/symbol_verify_log.json'
    with open(path,'r') as f:
        result = json.load(f)
    table = PrettyTable(['exchange','symbol','trading start time','feed data start time'])
    table.add_row(['','','(by web site kline)','(some may before trading)'])
    table.border = False
    table.align = 'l'
    for mea,symbols in result.items():
        for symbol,data in symbols.items() :
            if len(data["time"])>19:
                time = data["time"][:-10]
            else:
                time = data["time"]
            
            # print("%s           %s          %s" %(mea,symbol,time))
            
            table.add_row([mea,symbol,time,time])
    print(table)

if __name__ == '__main__':
    get_capture_time('20230803')


